package cn.com.css.taxInspection.workflow.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.entity.SysUser;
import cn.com.css.taxInspection.system.service.UserService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.entity.WorkflowInspectionHearEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowInspectionHearService;
import cn.com.css.taxInspection.workflow.service.WorkflowLogService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

/**
 * ${comments}
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2023-10-09 10:23:17
 */
@Controller
@RequestMapping("/workflow/book")
public class WorkflowBookController {

    @Autowired
    private WorkflowLogService workflowLogService;
    @Autowired
    private JcAjxxService jcAjxxService;
    @Autowired
    private HttpSessionService httpSessionService;
    @Autowired
    private UserService userService;
    @Autowired
    private WorkflowInspectionHearService workflowInspectionHearService;

    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "案件台账-列表")
    @PostMapping("/list")
    @ResponseBody
    @DS("oracle")
    public DataResult list(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) throws ParseException {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(WorkflowInspectionHearEntity::getInspectionTaxorg11, currentDeptId.substring(0, 9)).or()
                .like(WorkflowInspectionHearEntity::getHearTaxorg21, currentDeptId.substring(0, 9));
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "案件台账-列表-日志")
    @PostMapping("/listBylog")
    @ResponseBody
    @DS("oracle")
    public DataResult listBylog(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) throws ParseException {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String id = workflowInspectionHear.getRelevance();
        if (TsBqUtils.isNull(id)) {
            return DataResult.fail("relevance不能为NULL");
        }
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getRelevance, id);
        queryWrapper.eq(WorkflowLogEntity::getType, WorkflowConstants.LOG_TYPE_2);
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String type = workflowLogEntity.getType();
            if (type.equals(WorkflowConstants.LOG_TYPE_2)) {
                String relevance = workflowLogEntity.getRelevance();
                JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
                if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                    String nsrmc = jcAjxxEntity.getNsrmc();
                    String nsrsbh = jcAjxxEntity.getNsrsbh();
                    workflowLogEntity.setNsrmc(nsrmc);
                    workflowLogEntity.setNsrsbh(nsrsbh);
                }
            }
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
        }
        return DataResult.success(iPage);
    }
}
